package com.fare.taluo.core.domain.crawl.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.dayatang.domain.DataPage;
import com.dayatang.hibernate.EntityRepositoryHibernate;
import com.fare.taluo.core.domain.crawl.TaskTemplateEntity;
import com.fare.taluo.core.domain.crawl.TaskTemplateEntityRepository;
import com.fare.taluo.util.StringUtils;

@Repository
public class TaskTemplateEntityRepositoryImpl extends EntityRepositoryHibernate
		implements TaskTemplateEntityRepository {

	@Override
	public TaskTemplateEntity findTaskInfoEntity(long id) throws Exception {
		return super.get(TaskTemplateEntity.class, id);
	}

	@Override
	public void deleteTaskInfoEntity(TaskTemplateEntity ti) throws Exception {
		super.remove(ti);
	}

	@Override
	public void saveOrUpdateTaskInfoEntity(TaskTemplateEntity ti) throws Exception {
		super.save(ti);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TaskTemplateEntity> findTaskInfoEntityCriteriaList( String grabChannel,String fromCity,String toCity, int grabsopeBegin,int grabsopeEnd,
			Date validityFrom,int pageNum,int pageSize) {
		String hql = "from TaskTemplateEntity t where 1=1";
		String condition = "";

		List<Object> list = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(grabChannel)) {
			condition = condition + " and t.grabChannel = ?";
			list.add(grabChannel);
		}
		if(StringUtils.isNotEmpty(fromCity)) {
			condition = condition + " and t.fromCity = ?";
			list.add(fromCity);
		}
		if(StringUtils.isNotEmpty(toCity)) {
			condition = condition + " and t.toCity = ?";
			list.add(toCity);
		}
		if(null != validityFrom) {
			condition = condition + " and t.validityFrom = ?";
			list.add(validityFrom);
		}
		
		if(grabsopeBegin!=-1){
			condition = condition + " and t.grabScopeBegin = ?";
			list.add(grabsopeBegin);
		}
		
		if(grabsopeEnd!=-1){
			condition = condition + " and t.grabScopeEnd = ?";
			list.add(grabsopeEnd);
		}

		hql = hql + condition + " order by t.createTime desc ";
		System.out.println("----"+hql);
		try{
		DataPage page = find(hql, list.toArray(), pageNum, pageSize,DataPage.class);
		List<TaskTemplateEntity> lists = page.getPageData();
		TaskTemplateEntity a = new TaskTemplateEntity();
		return lists;
		}catch(Exception e){
			System.err.print("sfsdf");
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int countTaskInfoEntityCriteria(String grabChannel, String fromCity,
			String toCity,Date validityFrom, int grabsopeBegin,int grabsopeEnd) {
		String hql = "from TaskTemplateEntity t where 1=1";
		String condition = "";

		List<Object> list = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(grabChannel)) {
			condition = condition + " and t.grabChannel = ?";
			list.add(grabChannel);
		}
		if(StringUtils.isNotEmpty(fromCity)) {
			condition = condition + " and t.fromCity = ?";
			list.add(fromCity);
		}
		if(StringUtils.isNotEmpty(toCity)) {
			condition = condition + " and t.toCity = ?";
			list.add(toCity);
		}
		if(null != validityFrom){
			condition = condition + " and t.validityFrom = ?";
			list.add(validityFrom);
		}

		if(grabsopeBegin!=-1){
			condition = condition + " and t.grabScopeBegin = ?";
			list.add(grabsopeBegin);
		}
		
		if(grabsopeEnd!=-1){
			condition = condition + " and t.grabScopeEnd = ?";
			list.add(grabsopeEnd);
		}

		hql = hql + condition ;
		List<TaskTemplateEntity> lists = null;
		try{
			 lists = super.find(hql, list.toArray(),TaskTemplateEntity.class);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return lists.size();
	}


	@Override
	public List<TaskTemplateEntity> findTaskInfoByConsdions(String grabChannel,
			String fromCity, String toCity, int grabsopeBegin,int grabsopeEnd) {
		String hql = "from TaskTemplateEntity t";
		String condition = "";

		List<Object> list = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(grabChannel)) {
			String str[] = grabChannel.split(";");
			for (int i = 0; i < str.length; i++) {
				if(StringUtils.isNotEmpty(condition)){
					condition = condition + " or t.grabChannel = ?";
				}else{
					condition = condition + " where t.grabChannel = ?";
				}
				list.add(str[i]);
			}
		}
		if(StringUtils.isNotEmpty(fromCity)) {
			if(StringUtils.isNotEmpty(condition)){
				condition = condition + " and t.fromCity = ?";
			}else{
				condition = condition + " where t.fromCity = ?";
			}
			list.add(fromCity);
		}
		if(StringUtils.isNotEmpty(toCity)) {
			if(StringUtils.isNotEmpty(condition)){
				condition = condition + " and t.toCity = ?";
			}else{
				condition = condition + " where t.toCity = ?";
			}
			list.add(toCity);
		}

//		if (grabsope != 0) {
//			if(StringUtils.isNotEmpty(condition)){
//				condition = condition + " and t.grabsope = ?";
//			}else{
//				condition = condition + " where t.grabsope = ?";
//			}
//			list.add(grabsope);
//		}

		if(grabsopeBegin!=0){
			if(StringUtils.isNotEmpty(condition)){
				condition = condition + " and t.grabscopeBegin <= ?";
			}else{
				condition = condition + " where t.grabscopeBegin <= ?";
			}
			list.add(grabsopeBegin);
		}
		
		if(grabsopeEnd!=0){
			if(StringUtils.isNotEmpty(condition)){
				condition = condition + " and t.grabscopeEnd >= ?";
			}else{
				condition = condition + " where t.grabscopeEnd >= ?";
			}
			list.add(grabsopeBegin);
		}
		
		hql = hql + condition + " order by t.grabChannel asc";
		
		List<TaskTemplateEntity> lists = super.find(hql, list.toArray(),TaskTemplateEntity.class);
		return lists;
	}

	@Override
	public TaskTemplateEntity findTaskInfoEntityVo(String grabChannel,
			String fromCity, String toCity, Date validityFrom, Date validityTo,
			int grabsopeBegin, int grabsopeEnd) {
		TaskTemplateEntity entity=null;
		StringBuffer hql=new StringBuffer("from TaskTemplateEntity t where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(grabChannel)) {
			hql.append(" and t.grabChannel = ?");
			params.add(grabChannel);
		}
		if(StringUtils.isNotEmpty(fromCity)) {
			hql.append(" and t.fromCity = ?") ;
			params.add(fromCity);
		}
		if(StringUtils.isNotEmpty(toCity)) {
			hql.append(" and t.toCity = ?");
			params.add(toCity);
		}
		if(grabsopeBegin!=0){
			hql.append(" and t.grabScopeBegin = ?");
			params.add(grabsopeBegin);
		}
		if(grabsopeEnd!=0){
			hql.append(" and t.grabScopeEnd = ?");
			params.add(grabsopeEnd);
		}
		if(validityFrom!=null){
			hql.append("and to_char(t.validityFrom,'yyyy-mm-dd hh24:mi:ss') like ? ");
			SimpleDateFormat sf=
					new SimpleDateFormat("yyyy-MM-dd");
			params.add(sf.format(validityFrom)+"%");
		}
		if(validityTo!=null){
			hql.append("and to_char(t.validityTo,'yyyy-mm-dd hh24:mi:ss') like ? ");
			SimpleDateFormat sf=
					new SimpleDateFormat("yyyy-MM-dd");
			params.add(sf.format(validityTo)+"%");
		}
		
		List<TaskTemplateEntity> result = super.find(hql.toString(), params.toArray(),TaskTemplateEntity.class);
		if(result!=null && result.size()>0){
			entity=result.get(0);
		}
		
		return entity;
	}
	
	public boolean existTask(String fromCity,String toCity,String hbaseKey){
		TaskTemplateEntity entity=null;
		StringBuffer hql=new StringBuffer("from TaskTemplateEntity t where 1=1");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotEmpty(fromCity)) {
			
			hql.append(" and t.fromCity = ?");
			params.add(fromCity);
		}
		if(StringUtils.isNotEmpty(toCity)) {
			hql.append(" and t.toCity = ?") ;
			params.add(toCity);
		}
		if(StringUtils.isNotEmpty(hbaseKey)) {
			hql.append(" and t.attachHbaseKey = ?");
			params.add(hbaseKey);
		}
		
		List<TaskTemplateEntity> result = super.find(hql.toString(), params.toArray(),TaskTemplateEntity.class);
		if(result!=null && result.size()>0){
			return true;
		}else{
			return false;
		}
		
	}

	@Override
	public List<TaskTemplateEntity> findByChannelId(long grabChannelId) {
		String hql = "from TaskTemplateEntity t where t.grabChannelId="+grabChannelId;
		return this.getSession().createQuery(hql).list();
	}
	@Override
	public List<TaskTemplateEntity> findByHbaseKey(String attachHbaseKey) {
		String hql = "from TaskTemplateEntity t where t.attachHbaseKey="+attachHbaseKey;
		return this.getSession().createQuery(hql).list();
	}
	@Override
	public List<TaskTemplateEntity> findJDBCByHbaseKey(String attachHbaseKey) {
		List<TaskTemplateEntity> results = new ArrayList<TaskTemplateEntity>();
		String sql = "select distinct t.FROM_CITY,t.TO_CITY from CRAWL_TASK_TEMPLATE t where t.ATTACH_HBASE_KEY='"+attachHbaseKey+"' order by t.FROM_CITY,t.TO_CITY";
		List<Object[]> list = this.getSession().createSQLQuery(sql).list();
		TaskTemplateEntity entity = new TaskTemplateEntity();
		for(Object[] o : list){
			entity = new TaskTemplateEntity();
			entity.setFromCity(String.valueOf(o[0]));
			entity.setToCity(String.valueOf(o[1]));
			
			results.add(entity);
		}
		return results;
	}
	@Override
	public int findJDBCByHbaseKeys(String attachHbaseKey) {
		String sql = "select count(*) from CRAWL_TASK_TEMPLATE t where t.ATTACH_HBASE_KEY='"+attachHbaseKey+"' ";
		 int counts=Integer.parseInt(this.getSession().createSQLQuery(sql).list().get(0).toString());
		return counts;
	}
	
	@Override
	public List<TaskTemplateEntity> findTaskInfoByGrabChannel(String grabChannel) {
			String hql = "from TaskTemplateEntity t where 1=1";
			String condition = "";

			List<Object> list = new ArrayList<Object>();
			if(StringUtils.isNotEmpty(grabChannel)) {
				condition = condition + " and t.grabChannel = ?";
				list.add(grabChannel);
			}
			
			hql = hql + condition ;
			List<TaskTemplateEntity> lists = null;
			try{
				 lists = super.find(hql, list.toArray(),TaskTemplateEntity.class);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			return lists;
	}

	@Override
	public List<TaskTemplateEntity> findTaskInfoByConsdion(String grabChannelId, String fromCity, String toCity) {

		String hql = "from TaskTemplateEntity t where 1=1";
		String condition = "";

		List<Object> list = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(grabChannelId)) {
			condition = condition + " and t.grabChannelId = ?";
			list.add(Long.parseLong(grabChannelId));
		}
		if (StringUtils.isNotEmpty(fromCity)) {
			condition = condition + " and t.fromCity = ?";
			list.add(fromCity);
		}
		if (StringUtils.isNotEmpty(toCity)) {
			condition = condition + " and t.toCity = ?";
			list.add(toCity);
		}


		hql = hql + condition + " order by t.grabChannelId desc";

		List<TaskTemplateEntity> lists = super.find(hql, list.toArray(),
				TaskTemplateEntity.class);
		return lists;
	}

	
	
	
}
